import logging

from openlbsserver.lib.base import *

from openlbsserver.model import meta, Device, Location

log = logging.getLogger(__name__)

class TrackController(BaseController):

    def device(self, device_id):
        try:
            dev = meta.Session.query(Device).filter_by(user_id=session['user_id'],id=device_id).one()
            
            c.device = dev
            
            try:
                c.location = meta.Session.query(Location).filter_by(device_id=dev.id).order_by(Location.id.desc())[0]
            except:
                c.no_location = True
        
            return render('track_device')
        except Exception, msg:
            print msg
            abort(404)
